package com.transfiguro.utilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * This class provides static methods for file IO
 */
public class FileUtilities {
    private static final Logger logger = LoggerFactory.getLogger(FileUtilities.class);

    /**
     * This method takes an <code>InputStream</code> and writes to a file
     *
     * @param inputStream  The input stream to be written to a file
     * @param file         The file to be written to
     */
    public static void writeTempFile(final InputStream inputStream, final File file) {
        OutputStream outputStream;
        try {
            outputStream = new FileOutputStream(file);
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }

            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (final FileNotFoundException e) {
            logger.debug("Exception: {}", e.getMessage());
        } catch (IOException e) {
            logger.debug("Exception: {}", e.getMessage());
        }
    }
}
